﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Honestysoft.Workflow.Model;
using System.Data.Common;
using Honestysoft.Financing.Model;
using Honestysoft.Workflow.BusinessLogic;
using Honestysoft.BaseObject;
using Honestysoft.Baseframework.WebUI;

namespace Honestysoft.Financing.WebUI
{
    public class AuditTableActAcc : IWFActAccessorial
    {
        // 摘要:
        //     流程退回的后续操作(A发给B后,B执行了退回操作)
        //
        // 参数:
        //   pWFReturn:
        //
        //   pWFDbTransaction:
        public void WFBackAccessorial(WFReturn pWFReturn, DbTransaction pWFDbTransaction)
        {
            string dataId = pWFReturn.FromBuDatas[0].BuDataID;
            BaseObject.BaseBusinessLogic<Bu_AuditTable> bll = new BaseObject.BaseBusinessLogic<Bu_AuditTable>(Globals.CurrentConnStringName);
            Bu_AuditTable table = bll.GetEntity(dataId, pWFDbTransaction);
            if (pWFReturn.ToActTemplates != null && pWFReturn.ToActTemplates[0].ActType == ((int)WFEnumData.ActType.Start).ToString())
            {
                Honestysoft.BaseLibrary.Logging.LoggingHelper.WriteLog("执行了退回到初始操作！");
                //打回初始，要删除作业表及派单记录
                bll.DeleteByKey(dataId, pWFDbTransaction);
                QueryParameters query = new QueryParameters();
                query.Add("TableID", dataId, QueryParameters.OperatorType.Equal);
                new BaseBusinessLogic<Bu_BatchRec>().DeleteByWhere(query, pWFDbTransaction);
            }
            else
            {
                if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.NoRec).ToString())
                {
                    //将对应的批次回执设置为未派单
                    BaseBusinessLogic<Bu_BatchRec> bll2 = new BaseBusinessLogic<Bu_BatchRec>();
                    QueryParameters query = new QueryParameters();
                    query.Add("TableID", dataId, QueryParameters.OperatorType.Equal);
                    List<Bu_BatchRec> lsBatchRec = bll2.GetEntitys(query, pWFDbTransaction);
                    if (lsBatchRec != null && lsBatchRec.Count == 1)
                    {
                        lsBatchRec[0].IsSend = false;
                        lsBatchRec[0].SendTime = null;
                        lsBatchRec[0].ToUserID = null;
                        lsBatchRec[0].ToUserName = null;
                        lsBatchRec[0].IsReceive = false;
                        lsBatchRec[0].RecTime = null;
                        lsBatchRec[0].RecOpinion = "";
                        bll2.UpdateEntitys(lsBatchRec, pWFDbTransaction);
                    }
                    //删除该作业表流程实例(已收回到启动，自动删除了实例)
                }
                else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.FAppr).ToString())
                {
                    bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.NoRec).ToString(), dataId, pWFDbTransaction);
                    BaseBusinessLogic<Bu_BatchRec> bll2 = new BaseBusinessLogic<Bu_BatchRec>();
                    QueryParameters query = new QueryParameters();
                    query.Add("TableID", dataId, QueryParameters.OperatorType.Equal);
                    List<Bu_BatchRec> lsBatchRec = bll2.GetEntitys(query, pWFDbTransaction);
                    if (lsBatchRec != null && lsBatchRec.Count == 1)
                    {
                        lsBatchRec[0].IsSend = false;
                        lsBatchRec[0].SendTime = null;
                        lsBatchRec[0].ToUserID = null;
                        lsBatchRec[0].ToUserName = null;

                        lsBatchRec[0].IsReceive = false;
                        lsBatchRec[0].RecTime = null;
                        lsBatchRec[0].RecOpinion = "";
                        bll2.UpdateEntitys(lsBatchRec, pWFDbTransaction);
                    }
                }
                else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.SAppr).ToString())
                {
                    bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.FAppr).ToString(), dataId, pWFDbTransaction);
                }
                else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.Lock).ToString())
                {
                    bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.SAppr).ToString(), dataId, pWFDbTransaction);
                }
                else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.Output).ToString())
                {
                    bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.Lock).ToString(), dataId, pWFDbTransaction);
                }
            }
        }
        //
        // 摘要:
        //     流程收回的后续操作(A发给B后,B未处理时,A执行了收回操作)
        //
        // 参数:
        //   pWFReturn:
        //
        //   pWFDbTransaction:
        public void WFRecallAccessorial(WFReturn pWFReturn, DbTransaction pWFDbTransaction)
        {
            string dataId = pWFReturn.FromBuDatas[0].BuDataID;
            BaseObject.BaseBusinessLogic<Bu_AuditTable> bll = new BaseObject.BaseBusinessLogic<Bu_AuditTable>(Globals.CurrentConnStringName);
            Bu_AuditTable table = bll.GetEntity(dataId, pWFDbTransaction);

            if (pWFReturn.ToActTemplates != null && pWFReturn.ToActTemplates[0].ActType == ((int)WFEnumData.ActType.Start).ToString())
            {
                Honestysoft.BaseLibrary.Logging.LoggingHelper.WriteLog("执行了退回到初始操作！");
                //打回初始，要删除作业表及派单记录
                bll.DeleteByKey(dataId, pWFDbTransaction);
                QueryParameters query = new QueryParameters();
                query.Add("TableID", dataId, QueryParameters.OperatorType.Equal);
                new BaseBusinessLogic<Bu_BatchRec>().DeleteByWhere(query, pWFDbTransaction);
            }
            else
            {

                if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.NoRec).ToString())
                {
                    //将对应的批次回执设置为未派单
                    BaseBusinessLogic<Bu_BatchRec> bll2 = new BaseBusinessLogic<Bu_BatchRec>();
                    QueryParameters query = new QueryParameters();
                    query.Add("TableID", dataId, QueryParameters.OperatorType.Equal);
                    List<Bu_BatchRec> lsBatchRec = bll2.GetEntitys(query, pWFDbTransaction);
                    if (lsBatchRec != null && lsBatchRec.Count == 1)
                    {
                        lsBatchRec[0].IsSend = false;
                        lsBatchRec[0].SendTime = null;
                        lsBatchRec[0].ToUserID = null;
                        lsBatchRec[0].ToUserName = null;
                        lsBatchRec[0].IsReceive = false;
                        lsBatchRec[0].RecTime = null;
                        lsBatchRec[0].RecOpinion = "";
                        bll2.UpdateEntitys(lsBatchRec, pWFDbTransaction);
                    }
                    //删除该作业表流程实例(已收回到启动，自动删除了实例)
                }
                else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.FAppr).ToString())
                {
                    bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.NoRec).ToString(), dataId, pWFDbTransaction);
                    BaseBusinessLogic<Bu_BatchRec> bll2 = new BaseBusinessLogic<Bu_BatchRec>();
                    QueryParameters query = new QueryParameters();
                    query.Add("TableID", dataId, QueryParameters.OperatorType.Equal);
                    List<Bu_BatchRec> lsBatchRec = bll2.GetEntitys(query, pWFDbTransaction);
                    if (lsBatchRec != null && lsBatchRec.Count == 1)
                    {
                        lsBatchRec[0].IsSend = false;
                        lsBatchRec[0].SendTime = null;
                        lsBatchRec[0].ToUserID = null;
                        lsBatchRec[0].ToUserName = null;
                        lsBatchRec[0].IsReceive = false;
                        lsBatchRec[0].RecTime = null;
                        lsBatchRec[0].RecOpinion = "";
                        bll2.UpdateEntitys(lsBatchRec, pWFDbTransaction);
                    }
                }
                else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.SAppr).ToString())
                {
                    bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.FAppr).ToString(), dataId, pWFDbTransaction);
                }
                else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.Lock).ToString())
                {
                    bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.SAppr).ToString(), dataId, pWFDbTransaction);
                }
                else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.Output).ToString())
                {
                    bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.Lock).ToString(), dataId, pWFDbTransaction);
                }
            }
        }
        //
        // 摘要:
        //     流程启动的后续操作
        //
        // 参数:
        //   pWFReturn:
        //
        //   pWFDbTransaction:
        public void WFStartAccessorial(WFReturn pWFReturn, DbTransaction pWFDbTransaction)
        { 
            List<string> lsTableID=new List<string>();
            foreach(WFBuData item in pWFReturn.FromBuDatas)
            {
                lsTableID.Add(item.BuDataID);
            }
            BaseObject.BaseBusinessLogic<Bu_BatchRec> bllBatchRec = new BaseObject.BaseBusinessLogic<Bu_BatchRec>();
            QueryParameters query = new QueryParameters();
            query.Add("TableID", lsTableID, QueryParameters.OperatorType.IN);
            List<Bu_BatchRec> lsBatchRec = bllBatchRec.GetEntitys(query, pWFDbTransaction);
            DateTime now=DateTime.Now;
            foreach(Bu_BatchRec item in lsBatchRec)
            {
                item.IsSend=true;
                item.SendTime=now;
                item.ToUserID = pWFReturn.ToActivitys[0].GranterEmployee.UserID;
                item.ToUserName =  pWFReturn.ToActivitys[0].GranterEmployee.UserName;
            }
            bllBatchRec.UpdateEntitys(lsBatchRec,pWFDbTransaction);
        }
        //
        // 摘要:
        //     流程处理的后续操作(A发给B后执行的操作)
        //
        // 参数:
        //   pWFReturn:
        //
        //   pWFDbTransaction:
        public void WFTransAccessorial(WFReturn pWFReturn, DbTransaction pWFDbTransaction)
        {
            string dataId = pWFReturn.FromBuDatas[0].BuDataID;
            BaseObject.BaseBusinessLogic<Bu_AuditTable> bll = new BaseObject.BaseBusinessLogic<Bu_AuditTable>();
            Bu_AuditTable table = bll.GetEntity(dataId, pWFDbTransaction);
            if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.NoRec).ToString())//签收完成
            {
                bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.FAppr).ToString(), dataId, pWFDbTransaction);
                bll.UpdateColumn("FirstDate",DateTime.Now, dataId, pWFDbTransaction);
            }
            else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.FAppr).ToString())//审核完成
            {
                bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.SAppr).ToString(), dataId, pWFDbTransaction);
                bll.UpdateColumn("FirstDate",DateTime.Now, dataId, pWFDbTransaction);
            }
            else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.SAppr).ToString())//复核完成
            {
               bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.Lock).ToString(), dataId, pWFDbTransaction);
            }
            else if (table.BuStatus == ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.Lock).ToString())//完成，输出
            {
               bll.UpdateColumn("BuStatus", ((int)Honestysoft.Financing.Model.EnumItem.AuditTableStatus.Output).ToString(), dataId, pWFDbTransaction);
            }
        }
    }
}